package com.pinc.exam;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Library extends WidgetActivity {
	private ListView lv;
	private DBClass dbHelper;
	private Cursor mCursor;
	private Question_Adapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);

		long id = this.getIntent().getLongExtra("id", 0);

		dbHelper = new DBClass(this);
		mAdapter = new Question_Adapter(this);

		if ( id == -1)
			mCursor = dbHelper.getRecord(DBClass.TABLE_NAME_QLIB, null, null, null, null);
		else
			mCursor = dbHelper.getRecord(DBClass.TABLE_NAME_QLIB, DBClass.COL_QLIB_CATEGORY + " = ?", new String[]{String.valueOf(id)}, null, null);

		if (mCursor.moveToFirst())
		{
			do{
				Question_Class qc = new Question_Class(
						mCursor.getInt(mCursor.getColumnIndex("_id")),
						mCursor.getInt(mCursor.getColumnIndex("_id")),
						mCursor.getString(mCursor.getColumnIndex(DBClass.COL_QLIB_QUESTION)),
						mCursor.getInt(mCursor.getColumnIndex(DBClass.COL_QLIB_BINGO_NO)),
						mCursor.getInt(mCursor.getColumnIndex(DBClass.COL_QLIB_TESTED_NO)),
						mCursor.getFloat(mCursor.getColumnIndex(DBClass.COL_QLIB_CREDIT))
				);
				mAdapter.add(qc);
			}while(mCursor.moveToNext());
		}
		mCursor.close();
		dbHelper.mDB.close();

		initWidget();

		lv.setAdapter(mAdapter);
		lv.setOnItemClickListener(new ItemClickListener());
	}

	private class ItemClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
			Intent intent = new Intent(Library.this, Question_Content.class);
			intent.putExtra("title", getIntent().getStringExtra("title"));
			intent.putExtra("ids", mAdapter.getIdArray());
			intent.putExtra("pos", position);
			startActivity(intent);
		}
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		lv = (ListView) this.findViewById(R.id.listView1);

		/* set upbar */
		tv_title.setText(getIntent().getStringExtra("title"));
		b_prev.setText(getResources().getString(R.string.str_back));
		b_prev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		b_prev.setVisibility(View.VISIBLE);

		b_next.setText(getResources().getString(R.string.str_menu));
		b_next.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Library.this.openOptionsMenu();
			}
		});
		b_next.setVisibility(View.VISIBLE);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.library, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.sortby_rate:
			mAdapter.sort(1);
			lv.setAdapter(mAdapter);
			return true;
		case R.id.sortby_id:
			mAdapter.sort(0);
			lv.setAdapter(mAdapter);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
